package dao.impl;

import com.alibaba.druid.util.StringUtils;
import dao.OrderDao;
import model.bo.OrderQueryBO;
import model.po.Order;
import model.vo.OrderVO;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.DruidUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao {

    @Override
    public Order orderById(Integer id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
//        StringBuilder sql = new StringBuilder();
//        sql.append("select ")
//            .append("`id`, ")
//            .append("`user_id`, ")
//            .append("`spec_id`, ")
//            .append("`goods`, ")
//            .append("`spec`, ")
//            .append("`number`, ")
//            .append("`amount`, ")
//            .append("`state`, ")
//            .append("`nickname`, ")
//            .append("`recipient`, ")
//            .append("`address`, ")
//            .append("`phone` ")
//            .append(" from `order`")
//            .append(" where `id` = ?");
        String sql = "select * from `order` where `id` = ?";
        Order order = null;
        try {
            order = queryRunner.query(sql, new BeanHandler<>(Order.class, new BasicRowProcessor(new BeanProcessor(Order.MAPFROM))), id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return order;
    }

    @Override
    public int deleteOrder(Integer id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "delete from `order` where `id` = ?";
        int result = 0;
        try {
            result = queryRunner.update(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    @Override
    public List<OrderVO> orderVOsByPage(OrderQueryBO bo) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
//        String prefix = "select `id`, " +
//                "`user_id` as `userId`, " +
//                "`spec_id` as `goodsDetailId`, " +
//                "`goods`, " +
//                "`spec`, " +
//                "`number` as `goodsNum`, " +
//                "`amount`, " +
//                "`state` as `stateId`, " +
//                "`nickname`, " +
//                "`recipient` as `name`, " +
//                "`address`, " +
//                "`phone` ";
        String prefix = "select " +
                "`id`, " +
                "`user_id`, " +
                "`spec_id`, " +
                "`goods`, " +
                "`spec`, " +
                "`number`, " +
                "`amount`, " +
                "`state`, " +
                "`nickname`, " +
                "`recipient`, " +
                "`address`, " +
                "`phone` ";
        Map<String, Object> result = getDynamicSql(bo);
        List<OrderVO> orderVOS = null;
        String sql = (String) result.get("sql");
        List<Object> params = (List<Object>)result.get("parameters");

        String limit = " limit ? offset ?";
        params.add(bo.getPagesize());
        params.add((bo.getCurrentPage() - 1) * bo.getPagesize());
        try {
            orderVOS = queryRunner.query(prefix + sql + limit, new BeanListHandler<>(OrderVO.class, new BasicRowProcessor(new BeanProcessor(OrderVO.MAPFROM))), params.toArray());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orderVOS;
    }

    @Override
    public int getTotal(OrderQueryBO bo) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String prefix = "select count(*) ";
        Map<String, Object> rslt = getDynamicSql(bo);
        String sql = (String) rslt.get("sql");
        List<Object> params = (List<Object>)rslt.get("parameters");
        Long result = 0L;
        try {
            result = queryRunner.query(prefix + sql, new ScalarHandler<>(), params.toArray());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result.intValue();
    }

    private Map<String, Object> getDynamicSql(OrderQueryBO bo) {
        String base = " from `order` where 1 = 1";
        List<Object> params = new ArrayList<>();
        if (-1 != bo.getState()) {
            base += " and `state` = ?";
            params.add(bo.getState());
        }
        if (!StringUtils.isEmpty(bo.getAddress())) {
            base += " and `address` like ?";
            params.add("%" + bo.getAddress() + "%");
        }
        if (!StringUtils.isEmpty(bo.getName())) {
            base += " and `recipient` like ?";
            params.add("%" + bo.getName() + "%");
        }
        if (!StringUtils.isEmpty(bo.getMoneyLimit1())) {
            base += " and `amount` <= ?";
            params.add(bo.getMoneyLimit1());
        }
        if (!StringUtils.isEmpty(bo.getMoneyLimit2())) {
            base += " and `amount` >= ?";
            params.add(bo.getMoneyLimit2());
        }
        if (!StringUtils.isEmpty(bo.getGoods())) {
            base += " and `goods` like ?";
            params.add("%" + bo.getGoods() + "%");
        }
        if (!StringUtils.isEmpty(bo.getId())) {
            base += " and `id` = ?";
            params.add(bo.getId());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("sql", base);
        map.put("parameters", params);
        return map;
    }
}
